<?php
/**
 * @Notes:【】
 * @Date: 2022-06-05 15:25
 */

namespace App\Controller\Admin\Task;

use App\Controller\Admin\BaseController;
use App\Middleware\CheckAdminMiddleware;
use App\Service\Task\TaskReceiveService;
use App\Service\Task\TaskService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Middleware;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;

/**
 * @ApiController(prefix="admin/task/receive")
 * @Middleware(CheckAdminMiddleware::class)
 */
class TaskReceiveController extends BaseController
{
    /**
     * @Inject()
     * @var TaskService
     */
    protected $taskService;

    /**
     * @Inject()
     * @var TaskReceiveService
     */
    protected $taskReceiveService;

    /**
     * @Notes:【】
     * @Date: 2022-06-05 15:25
     * @PostApi(path="lists")
     * @FormData(key="is_recomment",rule="nullable")
     * @FormData(key="keyword",rule="nullable")
     * @FormData(key="status",rule="nullable")
     * @FormData(key="nickname",rule="nullable")
     * @FormData(key="phone",rule="nullable")
     * @FormData(key="pay_status",rule="nullable")
     */
    public function lists()
    {
        $params = $this->getParams(false);
        $data = $this->taskReceiveService->lists($params);
        _SUCCESS($data);
    }

    /**
     * @Notes:【添加】
     * @User: zwc
     * @Date: 2022-05-04 13:33
     * @PostApi(path="add")
     * @FormData(key="product_name",rule="Required")
     * @FormData(key="image_id",rule="nullable")
     * @FormData(key="title",rule="Required")
     * @FormData(key="desc",rule="Required")
     * @FormData(key="notice",rule="Required")
     * @FormData(key="stock",rule="Required")
     * @FormData(key="end_time",rule="Required|date")
     * @FormData(key="one_price",rule="Required")
     * @FormData(key="audit_cycle",rule="nullable")
     * @FormData(key="submit_cycle",rule="nullable")
     * @FormData(key="task_steps",rule="Required")
     * @FormData(key="info",rule="nullable")
     * @FormData(key="task_types_id",rule="Required")
     */
    public function add()
    {
        $data = $this->getData();
        $data->offsetSet('user_id', 10008);
        $task = $this->taskService->add($data);
        $this->taskService->setFieldStatus($task->id, 'pay_status', 1);
        $this->taskService->setFieldStatus($task->id, 'status', 2);
        _SUCCESS($task);
    }


    /**
     * @Notes:【】
     * @User: zwc
     * @Date: 2022-05-09 21:06
     * @PostApi(path="detail")
     * @FormData(key="task_id",rule="Required")
     */
    public function detail()
    {
        $validator = $this->getParams(false);
        $data = $this->taskService->detail($validator);
        _SUCCESS($data);
    }

    /**
     * @Notes:【】
     * @User: zwc
     * @Date: 2022-05-04 13:33
     * @PostApi(path="edit")
     * @FormData(key="image_id",rule="nullable")
     * @FormData(key="task_id",rule="Required")
     * @FormData(key="product_name",rule="nullable")
     * @FormData(key="title",rule="nullable")
     * @FormData(key="desc",rule="nullable")
     * @FormData(key="notice",rule="nullable")
     * @FormData(key="task_steps",rule="nullable")
     * @FormData(key="info",rule="nullable")
     * @FormData(key="type",rule="nullable")
     * @FormData(key="price",rule="nullable")
     * @FormData(key="one_price",rule="nullable")
     * @FormData(key="end_time",rule="nullable|date")
     * @FormData(key="stock",rule="nullable")
     * @FormData(key="audit_cycle",rule="nullable")
     * @FormData(key="submit_cycle",rule="nullable")
     * @FormData(key="device",rule="nullable")
     * @FormData(key="task_types_id",rule="nullable")
     */
    public function edit()
    {
        $data = $this->getValidatorData();
        $data->offsetSet('total_stock', $data->get('stock'));
        $task = $this->taskService->edit(
            $data->get('task_id'),
            $data->except('task_id')
        );
        _SUCCESS();
    }

    /**
     * @Notes:【】
     * @Date: 2022-06-06 0:08
     * @PostApi(path="audit")
     * @FormData(key="task_id")
     * @FormData(key="reason")
     * @FormData(key="status")
     */
    public function audit()
    {
        $this->taskService->audit($this->getData());
        _SUCCESS();
    }

    /**
     * @PostApi(path="set-field-status")
     * @FormData(key="id")
     * @FormData(key="value")
     * @FormData(key="field")
     */
    public function setFieldtatus()
    {
        $params = $this->getParams(false);
        $this->taskService->setFieldStatus($params->get('id'), $params->get('field'), $params->get('value'));
        _SUCCESS();
    }
}